package org.compression.controller;

import org.common.util.JWTHelpUtil;
import org.common.util.PasswordEncrypt;
import org.common.util.jwt.JWTInfo;
import org.compression.mapper.MemberMapper;
import org.compression.mapper.UserMapper;
import org.model.Member;
import org.model.auth.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/auth")
public class AuthServerController {
		
	@Autowired
	UserMapper mMemberMapper;
	
	@RequestMapping(value = "/getToken", method = RequestMethod.POST)
	public Object getToken(@RequestBody User mMember) throws Exception {
		mMember.setPassword(PasswordEncrypt.encodeByMd5(mMember.getPassword()));
		User dataSource=mMemberMapper.getUser(mMember);
		try {
			if(mMember.getPassword().equals(dataSource.getPassword())&&dataSource.getLoginName().equals(mMember.getLoginName())) {
				JWTInfo info=new JWTInfo(mMember.getLoginName(),mMember.getId()+"",mMember.getEmail());
				return JWTHelpUtil.getToken(info);
			}
			
		}catch(Exception e) {
			return "0000";
		}
		return "0000";

	}
	
	
	@RequestMapping(value = "/addUser", method = RequestMethod.POST)
	public Object addMember(@RequestBody User mMember) throws Exception {
		mMember.setPassword(PasswordEncrypt.encodeByMd5(mMember.getPassword()));
		long flag=mMemberMapper.getHashed(mMember);
		if(flag>0) {
			return "存在该用户";
		}else {
			mMemberMapper.insertSelective(mMember);
		}
		mMember.setPassword("");
		return mMember;
	}
	

}
